package jp.idahopotato.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

public class SelectSkillGroupActivity extends ExpandableListActivity {
	CreateProductHelper helper = null;
	SQLiteDatabase db = null;
	String num = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);

		//前の画面から受け取った文字列の取得
		Intent data = getIntent();
		Bundle extras = data.getExtras();
		String str = extras.getString("SELECT_SKILL");
		num = extras.getString("NUM");
		TextView title = new TextView(this);
		title.setText(str);

		helper = new CreateProductHelper(SelectSkillGroupActivity.this);

		try{
			//データベースを開く
			db = helper.getReadableDatabase();
			String columns[] = {"skill_group_id", "skill_group_name"};
			Cursor cursor = db.query("m_skill_group", columns, null, null, null, null, "skill_group_id asc", null);

			//スキル系統名の取得と、親グループの表示
			List<Map<String, String>> pItemList = new ArrayList<Map<String, String>>();
			if(cursor.moveToFirst()){
				for(int i=0; i<cursor.getCount(); i++) {
					Map<String, String> item = new HashMap<String, String>();
					item.put("SKILL_GROUP", cursor.getString(1));
					pItemList.add(item);
					cursor.moveToNext();
				}
			}

			//各グループのスキル名の取得と、子リストの表示
			String columns2[] = {"skill_id", "skill_name"};
			Cursor cursor2;
			//アダプタ登録用全リスト
			List<List<Map<String, String>>> allSubItemList = new ArrayList<List<Map<String, String>>>();
			if(cursor.moveToFirst()){
				SQLiteQueryBuilder build = new SQLiteQueryBuilder();
				build.setTables("m_skill LEFT JOIN m_skill_group ON(m_skill.skill_group_id = m_skill_group.skill_group_id)");
				//検索条件は親のスキル系統名
				String where = "m_skill_group.skill_group_id = ?";
				for(int i=0; i<cursor.getCount(); i++) {
					String[] param = {cursor.getString(0)};
					String sql = build.buildQuery(columns2, where, param, null, null, "skill_id ASC", null);
					cursor2 = db.rawQuery(sql, param);

					List<Map<String, String>> subItemList = new ArrayList<Map<String, String>>();
					cursor2.moveToFirst();
					for(int j=0; j<cursor2.getCount(); j++) {
						Map<String, String> subItem = new HashMap<String, String>();
						subItem.put("SKILL_ID", cursor2.getString(0));
						subItem.put("SKILL_NAME", cursor2.getString(1));
						subItemList.add(subItem);
						cursor2.moveToNext();
					}
					allSubItemList.add(subItemList);
					cursor.moveToNext();
				}
			}

			//アダプタを作成
			SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
					this,
					pItemList,
					android.R.layout.simple_expandable_list_item_1,
					new String[] {"SKILL_GROUP"},
					new int[]{android.R.id.text1},
					allSubItemList,
					android.R.layout.simple_expandable_list_item_2,
					new String[]{"SKILL_ID", "SKILL_NAME"},
					new int[]{0, android.R.id.text1}
			);
			//アダプタを設定
			setListAdapter(adapter);

		} catch(Exception e){
			Log.e("ERROR", e.toString());
		}
		db.close();
	}

	// スキルが選択されたときの処理
	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
		// TODO 自動生成されたメソッド・スタブ
		ExpandableListAdapter adapter = parent.getExpandableListAdapter();
		// アダプタから選択されたスキルのデータを取得
		@SuppressWarnings("unchecked")
		Map<String, String> subItem = (Map<String, String>)adapter.getChild(groupPosition, childPosition);
		Log.v("スキル選択", subItem.get("SKILL_NAME") + " / " + subItem.get("SKILL_ID"));
		Intent intent = getIntent();
		intent.putExtra("RETURN_FLAG", true);
		intent.putExtra("SKILL_NAME", subItem.get("SKILL_NAME"));
		intent.putExtra("SKILL_ID", subItem.get("SKILL_ID"));
		intent.putExtra("RETURN_ID", num);
		setResult(RESULT_OK, intent);
		finish();

		return false;
	}


}
